package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.service.CheckItemService;
import com.xrit.sunnyday.common.MessageConstant;
import com.xrit.sunnyday.common.PageResult;
import com.xrit.sunnyday.common.QueryPageBean;
import com.xrit.sunnyday.common.Result;
import com.xrit.sunnyday.pojo.CheckItem;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkItem")
public class CheckitemController {
    //远程调用服务
    @Reference
    private CheckItemService checkItemService;
    //查询所有检查项
    @GetMapping  ("/findAll.do")
    public Result findAll(){
        List<CheckItem> checkItemList = checkItemService.findAll();
        Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        return result;
    }
    //查询所有检查项并分页
    @RequestMapping("/findAllPage.do")
    public Result findAllPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageList = checkItemService.findPageAll(queryPageBean);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageList);
    }
    //添加检查项
    @RequestMapping ("/save.do")
    @PreAuthorize("hasAnyAuthority('aa')")
    public Result save(@RequestBody CheckItem checkItem){
        //调用service添加方法
        checkItemService.save(checkItem);
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }
    //删除检查项
    @RequestMapping("/deleteById.do")
    public Result deleteById(Integer id){
        checkItemService.delete(id);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    //根据id查询检查项
    @GetMapping("/findById.do")
    public Result findById(Integer id){
        CheckItem checkItem = checkItemService.findById(id);
        System.out.println(checkItem);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);

    }
    //根据id修改
    @RequestMapping("/update.do")
    public Result update(@RequestBody CheckItem checkItem){
        checkItemService.update(checkItem);
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
}
